using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using TD.SandBar;
using NWN2Toolset.NWN2.UI;
using NWN2Toolset;
using NWN2Toolset.NWN2;
using NWN2Toolset.NWN2.NetDisplay;

namespace HotkeysPlugin
{
    class HotkeysAssignment
    {
        private static ButtonItemBase[] buttonsToolbar1 = new ButtonItemBase[18];
        private static ImageItemBase[] buttonsToolbar2 = new ImageItemBase[13];
        private static MenuButtonItem[] buttonsDayNight = new MenuButtonItem[11];
        private static MenuButtonItem[] buttonsShowHideFilter = new MenuButtonItem[20];
        private static MenuButtonItem[] buttonsSelectionFilter = new MenuButtonItem[20];

        // Test form
        DebugForm form1 = new DebugForm ();

        public HotkeysAssignment () {
            listButtons ();
        }

        private void listButtons () {
            Type type = NWN2ToolsetMainForm.App.GetType ();
            NWN2ToolsetMainForm app = NWN2ToolsetMainForm.App;
            FieldInfo[] fields = typeof (NWN2ToolsetMainForm).GetFields (BindingFlags.NonPublic | BindingFlags.Instance);

            foreach (FieldInfo info in fields) {
                if (info.FieldType == typeof (GraphicsPreferencesToolBar)) {
                    GraphicsPreferencesToolBar bar = info.GetValue (app) as GraphicsPreferencesToolBar;
                    int count = 0;
                    foreach (ButtonItemBase button in bar.Items) {
                        buttonsToolbar1[count] = button;

                        if (count == 15) {
                            DropDownMenuItem dropDown = (DropDownMenuItem) button;
                            int count2 = 0;
                            foreach (MenuButtonItem menuButton in dropDown.Items) {
                                buttonsDayNight[count2] = menuButton;
                                count2++;
                            }
                        }
                        count++;
                    }
                }
                if (info.FieldType == typeof (TD.SandBar.ToolBar)) {
                    TD.SandBar.ToolBar bar = info.GetValue (app) as TD.SandBar.ToolBar;
                    if (bar.Name == "toolBarObjectManipulation") {
                        int count = 0;
                        foreach (ImageItemBase button in bar.Items) {
                            buttonsToolbar2[count] = button;

                            if (count == 1) {
                                DropDownMenuItem dropDown = (DropDownMenuItem) button;
                                int count2 = 0;
                                foreach (MenuButtonItem menuButton in dropDown.Items) {
                                    buttonsShowHideFilter[count2] = menuButton;
                                    count2++;
                                }
                            }
                            if (count == 2) {
                                DropDownMenuItem dropDown = (DropDownMenuItem) button;
                                int count2 = 0;
                                foreach (MenuButtonItem menuButton in dropDown.Items) {
                                    buttonsSelectionFilter[count2] = menuButton;
                                    count2++;
                                }
                            }
                            count++;
                        }
                    }
                }
            }
            return;
        }

        public void OnKeyDown (KeyEventArgs eArgs) {

            #region Toolbar 1
            if (eArgs.KeyData == PrefsManager.prefs.TbGrid)
                buttonsToolbar1[0].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbOcclusionGrid)
                buttonsToolbar1[1].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbSurfaceMesh)
                buttonsToolbar1[3].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbBaked)
                buttonsToolbar1[4].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbWireframe)
                buttonsToolbar1[5].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbSkeletons)
                buttonsToolbar1[6].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbShadows)
                buttonsToolbar1[7].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbWater)
                buttonsToolbar1[8].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbNormalMaps)
                buttonsToolbar1[9].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbFrameRate)
                buttonsToolbar1[10].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbBloom)
                buttonsToolbar1[11].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbFarPlane)
                buttonsToolbar1[12].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbFog)
                buttonsToolbar1[13].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbSky)
                buttonsToolbar1[14].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbMusic)
                buttonsToolbar1[17].PerformActivate ();
            #endregion

            #region Toolbar 2
            else if (eArgs.KeyData == PrefsManager.prefs.TbSnap)
                buttonsToolbar2[3].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbSelectObjects)
                buttonsToolbar2[4].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbPaintObjects)
                buttonsToolbar2[5].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbSelectTerrain)
                buttonsToolbar2[6].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbPaintTerrain)
                buttonsToolbar2[7].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbPaintSpawnPoint)
                buttonsToolbar2[8].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbCreateTransition)
                buttonsToolbar2[10].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbTiles)
                buttonsToolbar2[11].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.TbDragSelection)
                buttonsToolbar2[12].PerformActivate ();
            #endregion

            #region Day/Night stages
            else if (eArgs.KeyData == PrefsManager.prefs.DNSunrise)
                buttonsDayNight[0].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNDaytime)
                buttonsDayNight[1].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNSunset)
                buttonsDayNight[2].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNMoonrise)
                buttonsDayNight[3].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNNighttime)
                buttonsDayNight[4].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNMoonset)
                buttonsDayNight[5].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNTransition)
                buttonsDayNight[6].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNDefault)
                buttonsDayNight[7].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNRun)
                buttonsDayNight[8].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.DNNormalFastSpeedToggle) {
                if (!buttonsDayNight[9].Checked) {
                    buttonsDayNight[9].PerformActivate ();
                    buttonsDayNight[9].Checked = true;
                    buttonsDayNight[10].Checked = false;
                    NWN2ToolsetPreferences.Instance.Graphics.FastDayNightCycle = false;
                }
                else {
                    buttonsDayNight[10].PerformActivate ();
                    buttonsDayNight[9].Checked = false;
                    buttonsDayNight[10].Checked = true;
                    NWN2ToolsetPreferences.Instance.Graphics.FastDayNightCycle = true;
                }
            }
            #endregion

            #region Show/Hide filters
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide01ShowAll)
                buttonsShowHideFilter[0].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide02HideAll)
                buttonsShowHideFilter[1].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide03Creatures)
                buttonsShowHideFilter[2].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide04Doors)
                buttonsShowHideFilter[3].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide05Encounters)
                buttonsShowHideFilter[4].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide06Items)
                buttonsShowHideFilter[5].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide07Placeables)
                buttonsShowHideFilter[6].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide08Sounds)
                buttonsShowHideFilter[7].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide09StaticCameras)
                buttonsShowHideFilter[8].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide10Stores)
                buttonsShowHideFilter[9].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide11Triggers)
                buttonsShowHideFilter[10].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide12Waypoints)
                buttonsShowHideFilter[11].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide13Trees)
                buttonsShowHideFilter[12].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide14EnvironmentObjects)
                buttonsShowHideFilter[13].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide15Lights)
                buttonsShowHideFilter[14].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide16PlacedEffects)
                buttonsShowHideFilter[15].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide17PlacedEffectHelpers)
                buttonsShowHideFilter[16].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide18HookPoints)
                buttonsShowHideFilter[17].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Showhide19LightSpheres)
                buttonsShowHideFilter[18].PerformActivate ();

                /*
                  {
                NWN2InstanceFilter none = NWN2InstanceFilter.None;
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Creature;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Door;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Encounter;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Item;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Placeable;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Sound;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.StaticCamera;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Store;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Trigger;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Waypoint;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Tree;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Environment;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.Light;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.PlacedEffect;
                }
                if (this.Checked) {
                    none |= NWN2InstanceFilter.PlacedEffectHelper;
                }
                if (!this.Checked) {
                    NWN2NetDisplayManager.Instance.ShowLightSpheres = false;
                }
                NWN2NetDisplayManager.Instance.SetSpawnPointVisibility (this.Checked);
                NWN2NetDisplayManager.Instance.InstanceFilter = none;
            }
                 */
            #endregion

            #region Selection filters
            else if (eArgs.KeyData == PrefsManager.prefs.Selection01All)
                buttonsSelectionFilter[0].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection02None)
                buttonsSelectionFilter[1].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection03Creatures)
                buttonsSelectionFilter[2].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection04Doors)
                buttonsSelectionFilter[3].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection05Encounters)
                buttonsSelectionFilter[4].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection06Items)
                buttonsSelectionFilter[5].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection07Placeables)
                buttonsSelectionFilter[6].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection08Sounds)
                buttonsSelectionFilter[7].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection09StaticCameras)
                buttonsSelectionFilter[8].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection10Stores)
                buttonsSelectionFilter[9].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection11Triggers)
                buttonsSelectionFilter[10].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection12Waypoints)
                buttonsSelectionFilter[11].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection13Trees)
                buttonsSelectionFilter[12].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection14PlacedEffects)
                buttonsSelectionFilter[13].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection15EnvironmentObjects)
                buttonsSelectionFilter[14].PerformActivate ();
            else if (eArgs.KeyData == PrefsManager.prefs.Selection16Lights)
                buttonsSelectionFilter[15].PerformActivate ();
            #endregion


            /*  Meant for testing
            if (eArgs.KeyData == Keys.M) {
                form1.Show ();
                for (int i = 0 ; i < buttonsToolbar2.Length ; i++) {
                    if (buttonsToolbar2[i] != null)
                        form1.textBox1.Text += i + ". " + buttonsToolbar2[i].Text + " - ";
                }
            }
            */

            return;
        }


    }
}
